package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.UnlikeService;
import com.heima.common.constants.BehaviorConstants;
import com.heima.model.behavior.ApUserBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.ApThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class UnlikeServiceImpl implements UnlikeService {
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 此类型不喜欢
     * @param dto
     * @return
     */
    @Override
    public ResponseResult unLike(ApUserBehaviorDto dto) {
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //查看线程用户
        ApUser apUser = ApThreadLocalUtil.getApUser();
        //游客登录   结束
        if (apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        if (dto.getType()==0){
            redisTemplate.opsForHash().put(apUser.getId().toString(),
                    BehaviorConstants.UN_LIKE_BEHAVIOR+dto.getArticleId(), JSON.toJSONString(dto.getType()));
        }else {
            redisTemplate.opsForHash().delete(apUser.getId().toString(),
                    BehaviorConstants.UN_LIKE_BEHAVIOR+dto.getArticleId());
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
